<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :default-active="activePath"
          :collapse-transition="false"
          :collapse="isCollapse"
          :router="true"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="(item, index) in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + sunItem.path"
              v-for="(sunItem, index2) in item.children"
              :key="sunItem.id"
              @click="saveNavPath('/' + sunItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ sunItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
export default {
  data() {
    return {
      menulist: [],
      activePath: '',
      isCollapse: false,
      iconObj: {
        125: 'iconfont iconuser',
        103: 'iconfont iconkongjian',
        101: 'iconfont iconshangpin',
        102: 'iconfont icondanju-tianchong',
        145: 'iconfont iconbaobiao'
      }
    }
  },
  computed: {
    ...mapState(['activeNavPath'])
  },
  created() {
    this.getMenuList(), (this.activePath = this.activeNavPath)
  },
  methods: {
    ...mapMutations(['removeToken', 'saveActivePath', 'removeActivePath']),
    logout() {
      this.removeToken()
      this.removeActivePath()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$axios.get('menus')
      // console.log(data)
      this.menulist = res.data
    },
    saveNavPath(activePath) {
      this.saveActivePath(activePath)
      this.activePath = activePath
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="scss" scoped>
.home_container {
  height: 100%;
  .el-header {
    color: #fff;
    font-size: 20px;
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    div {
      display: flex;
      justify-content: space-between;
      align-items: center;
      img {
        margin-right: 10px;
      }
    }
  }
  .el-container {
    .el-aside {
      background-color: #333744;
      z-index: 1;
      .el-menu {
        border-right: none;
        .el-submenu__title {
          font-size: 14px;
          padding: 0 20px;
          box-sizing: border-box;
          .iconfont {
            margin-right: 10rpx;
          }
        }
      }
      .toggle-button {
        background-color: #4a5064;
        font-size: 10px;
        line-height: 20px;
        color: #fff;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
}
</style>
